<?php

/*
 * Custom Exception Class
 * ===============================================
 * Date: March 17th, 2012
 * Created By: Integration Team
 * ===============================================
 */

/**
 * custom Exception class to add additional functionality 
 * of logging and emailing on exceptions 
 */

require_once 'ExceptionFileWriterClass.php';
require_once 'ExceptionEmailerClass.php';

class customException extends Exception{
    
    
    
    //Exception codes
    //0 - Unspecified - Will be treated as Critical
    //1 - Critical - The script must stop, an email should be generated and 
    //               the exception should be emailed
    //2 - non-Critical - This is note-worthy but not fatal, needs to be logged 
    //                   Examples can include no price or cost higher than price                      
    
    public function __construct($message="", $code=0, Exception $previous=NULL) 
    
    {
        parent::__construct($message, $code, $previous);
        
        //Check the criticality of the error 
        if($code ==0 || $code == 1)
        {
            //attempt to log first - if it fails email
            try
            {
            $this->log_error($message);
            }
            catch (Exception $e)
            {
                echo "Failed to log Exception - ".$e->getMessage().$e->getTraceAsString().Line_Terminator;
                
                //Attempt to at least email
                try 
                {
                    $this->email_error($message);
                }
                catch(Exception $e)
                {
                    echo "Failed to log and email Exception... Exiting ".$e->getMessage().$e->getTraceAsString();
                    exit(1);
                }
            }
            
            //Logging was successful - try emailing
            
            try
            {
                $this->email_error($message);
            }
            catch (Exception $e)
            {
                    echo "Failed to log email Exception... Exiting ".$e->getMessage().$e->getTraceAsString();
                    exit(1);
            }
            //both email and log were successful - since this is critical exit
            echo "Fatal Exception caught - ".$message;
            exit(1);
        }
        else
        {
            //Not a fatal exception - log, email and move on 
            
            //attempt to log first - if it fails email
            try
            {
            $this->log_error($message);
            }
            catch (Exception $e)
            {
                echo "Failed to log Exception - ".$e->getMessage().$e->getTraceAsString();
                
                //Attempt to at least email
                try 
                {
                    $this->email_error($message);
                }
                catch(Exception $e)
                {
                    echo "Failed to log and email Exception... Exiting ".$e->getMessage().$e->getTraceAsString();
                    exit(1);
                }
            }
            
            //Logging was successful - try emailing
            
            try
            {
                $this->email_error($message);
            }
            catch (Exception $e)
            {
                    echo "Failed to log email Exception... Exiting ".$e->getMessage().$e->getTraceAsString();
                    exit(1);
            }
        }
        
    }
    private function log_error($message)
    {
        //Attempt to log
        $fileLogger = New ExceptionFileWriter("Errors.log", 'logs/', 'a', FALSE);
        $fileLogger->write(date("Y-m-d h:i:s a")."\t Exception \t".$message."\r\n");
    }
    private function email_error($message)
    {
        //Attempt to email
        $emailer = new ExceptioneMailer($message);
        $emailer->sendEmail($message);
    }
    
    


    
    
}

?>
